<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		ScriptPlanet.eu															/
/	Site:			http://www.scriptplanet.eu												/
/	Email:			support@scriptplanet.eu													/
/	Support:		http://www.scriptplanet.eu/support										/
/	Copyright:		2011 (c) ScriptPlanet.eu												/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0001																	/
/																							/
/========================================== JooSam ========================================*/

// Joomla Security
defined('_JEXEC') or die;

## Import Joomla Data
jimport('joomla.application.component.controllerform');
jimport('joomla.database.table');
 
## JooSam Event Controller
class JooSamControllerEvent extends JControllerForm{
	protected $text_prefix = 'Event';
	
	## On save click
	public function save($key = null, $urlVar = null){

		$data = JRequest::getVar('jform', array(), 'post', 'array');

		$id = $data['id'];
		$name = $data['spname'];
		$start = $data['spstart'];
		$end = $data['spends'];
		$rep = $data['sprepeat'];
		$req = $data['sprequests'];
		$info = $data['spinfo'];

		## Insert new Event
		if ($id == 0){
			$db =& JFactory::getDBO();
			$query = "
					INSERT INTO #__joosam_events (spname,spstart,spends,sprepeat,sprequests,spinfo)
					VALUES (".$db->quote($name).",".$db->quote($start).",".$db->quote($end).",".$db->quote($rep).",".$db->quote($req).",".$db->quote($info).")";
			$db->setQuery($query);
			$rest = $db->query();
			if (!$rest){
				$this->setMessage(JText::_('COM_JOOSAM_ERROR_SAVE_DATA'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=events', false));
			}
			else{
				$this->setMessage(JText::_('COM_JOOSAM_EVENT_ADDED'));
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=events', false));
			}
		}

		## Edit a Event
		else{
			$db =& JFactory::getDBO();
			$query = "
				UPDATE ".$db->nameQuote('#__joosam_events')."
				SET ".$db->nameQuote('spname')." = ".$db->quote($data['spname']).",
					".$db->nameQuote('spstart')." = ".$db->quote($data['spstart']).",
					".$db->nameQuote('spends')." = ".$db->quote($data['spends']).",
					".$db->nameQuote('sprepeat')." = ".$db->quote($data['sprepeat']).",
					".$db->nameQuote('sprequests')." = ".$db->quote($data['sprequests']).",
					".$db->nameQuote('spinfo')." = ".$db->quote($data['spinfo'])."
				WHERE ".$db->nameQuote('id')." = ".$db->quote($data['id']).";
			";
			$db->setQuery($query);
			$rest = $db->query();
			if (!$rest){
				$this->setMessage(JText::_('COM_JOOSAM_ERROR_SAVE_DATA'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=events', false));
			}
			else{
				$this->setMessage(JText::_('COM_JOOSAM_EVENT_SAVED'));
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=events', false));
			}
		}
	}
}